package org.xenon.vote.dao.imp;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import org.xenon.vote.dao.StringDao;
import org.xenon.vote.model.pojo.po.Judge;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@Repository
public class JudgeDao implements StringDao<Judge> {

    private final JdbcTemplate jdbcTemplate;

    public JudgeDao(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Override
    public void add(Judge judge) {
        String sql = "insert into `vote`.`judge` (`name`, `password`, `turn`, `owner`) values (?, ?, ?, ?)";
        jdbcTemplate.update(sql, judge.getName(), judge.getPassword(), judge.getTurn(), judge.getOwner());
    }

    @Override
    public void edit(Judge judge) {
        String sql = "update `vote`.`judge` set `password` = ? , `turn` = ?, `owner` = ? where `name` = ?";
        jdbcTemplate.update(sql, judge.getPassword(), judge.getTurn(), judge.getOwner(), judge.getName());
    }

    @Override
    @Deprecated
    public Judge get(String id) {
        return null;
    }


    public Judge get(String name, String owner) {
        String sql = "select * from `vote`.`judge` where `name` = ? and `owner` = ?";
        List<Judge> list = jdbcTemplate.query(sql, new Object[]{name, owner}, JudgeRowMapper.getInstance());
        if (list.size() == 0) return null;
        return list.get(0);
    }

    /**
     *
     * @param turn 所属的投票号
     * @return 所有该投票的裁判
     */
    @Deprecated
    @Override
    public List<Judge> getAll(String turn) {
        String sql = "select * from `vote`.`judge` where `turn` = ?";
        return jdbcTemplate.query(sql, new Object[]{Integer.parseInt(turn)}, JudgeRowMapper.getInstance());
    }

    public List<Judge> getAll(int turn) {
        String sql = "select * from `vote`.`judge` where `turn` = ?";
        return jdbcTemplate.query(sql, new Object[]{turn}, JudgeRowMapper.getInstance());
    }

    @Override
    public void del(Judge judge) {
        String sql = "delete FROM `vote`.`judge` WHERE `name` = ?";
        jdbcTemplate.update(sql, judge.getName());
    }

    @Override
    public void del(String name) {
        String sql = "delete FROM `vote`.`judge` WHERE `name` = ?";
        jdbcTemplate.update(sql, name);
    }

    /**
     * 清除某一轮的所有裁判
     * @param id 目标轮次
     */
    public void delAll(int id) {
        String sql = "delete FROM `vote`.`judge` WHERE `turn` = ?";
        jdbcTemplate.update(sql, id);
    }

    /**
     * 自动以RowMapper
     * 提高性能, 使用单例模式
     */
    private static class JudgeRowMapper implements RowMapper<Judge> {

        private static JudgeRowMapper judgeRowMapper;
        @Override
        public Judge mapRow(ResultSet rs, int i) throws SQLException {
            return new Judge(rs.getString("name"),
                    rs.getString("password"),
                    rs.getInt("turn"),
                    rs.getString("owner"));
        }

        static JudgeRowMapper getInstance(){
            if (judgeRowMapper == null){
                synchronized (JudgeRowMapper.class){
                    if(judgeRowMapper==null){
                        judgeRowMapper = new JudgeRowMapper();
                    }
                }
            }
            return judgeRowMapper;
        }
    }
}
